package com.timingbar.safe.library.view.recyclerview.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.*;
import com.timingbar.safe.library.view.imageloader.ImageLoader;
import com.timingbar.safe.library.view.imageloader.glide.GlideImageConfig;

/**
 * ViewHolder
 * -----------------------------------------------------------------------------------------------------------------------------------
 *
 * @author rqmei on 2018/2/28
 */

public class ViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViews;
    private View mConvertView;

    public ViewHolder(View itemView) {
        super (itemView);
        mConvertView = itemView;
        mViews = new SparseArray<View> ();
    }

    /**
     * 创建新View，被LayoutManager所调用
     *
     * @param itemView
     * @return
     */
    public static ViewHolder createViewHolder(View itemView) {
        ViewHolder holder = new ViewHolder (itemView);
        return holder;
    }

    public static ViewHolder createViewHolder(Context context, ViewGroup parent, int layoutId) {
        View itemView = LayoutInflater.from (context).inflate (layoutId, parent,
                false);
        ViewHolder holder = new ViewHolder (itemView);
        return holder;
    }

    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get (viewId);
        if (view == null) {
            view = mConvertView.findViewById (viewId);
            mViews.put (viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }


    /****以下为辅助方法*****/

    /**
     * 设置TextView的值
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView tv = getView (viewId);
        tv.setText (text);
        return this;
    }

    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView view = getView (viewId);
        view.setImageResource (resId);
        return this;
    }

    /**
     * 通过url路径加载图片
     *
     * @param imageLoader 加载图片的工具类
     * @param viewId      imageView对应的id
     * @param url         图片的路径
     * @return
     */
    public ViewHolder setImageUrl(Context mContext, ImageLoader imageLoader, int viewId, String url) {
        ImageView view = getView (viewId);
        imageLoader.loadImage (mContext, GlideImageConfig.builder ().imageView (view).transformationType (0).isClearMemory (true).isClearDiskCache (true).url (url).build ());
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView (viewId);
        view.setImageBitmap (bitmap);
        return this;
    }

    public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView (viewId);
        view.setImageDrawable (drawable);
        return this;
    }

    public ViewHolder setBackgroundColor(int viewId, int color) {
        View view = getView (viewId);
        view.setBackgroundColor (color);
        return this;
    }

    public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = getView (viewId);
        view.setBackgroundResource (backgroundRes);
        return this;
    }

    public ViewHolder setTextColor(int viewId, int textColor) {
        TextView view = getView (viewId);
        view.setTextColor (textColor);
        return this;
    }

    public ViewHolder setTextColorRes(Context mContext, int viewId, int textColorRes) {
        TextView view = getView (viewId);
        view.setTextColor (mContext.getResources ().getColor (textColorRes));
        return this;
    }

    @SuppressLint("NewApi")
    public ViewHolder setAlpha(int viewId, float value) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getView (viewId).setAlpha (value);
        } else {
            // Pre-honeycomb hack to set Alpha value
            AlphaAnimation alpha = new AlphaAnimation (value, value);
            alpha.setDuration (0);
            alpha.setFillAfter (true);
            getView (viewId).startAnimation (alpha);
        }
        return this;
    }

    public ViewHolder setVisible(int viewId, boolean visible) {
        View view = getView (viewId);
        view.setVisibility (visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public ViewHolder linkify(int viewId) {
        TextView view = getView (viewId);
        Linkify.addLinks (view, Linkify.ALL);
        return this;
    }

    public ViewHolder setTypeface(Typeface typeface, int... viewIds) {
        for (int viewId : viewIds) {
            TextView view = getView (viewId);
            view.setTypeface (typeface);
            view.setPaintFlags (view.getPaintFlags () | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    public ViewHolder setProgress(int viewId, int progress) {
        ProgressBar view = getView (viewId);
        view.setProgress (progress);
        return this;
    }

    public ViewHolder setProgress(int viewId, int progress, int max) {
        ProgressBar view = getView (viewId);
        view.setMax (max);
        view.setProgress (progress);
        return this;
    }

    public ViewHolder setMax(int viewId, int max) {
        ProgressBar view = getView (viewId);
        view.setMax (max);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating) {
        RatingBar view = getView (viewId);
        view.setRating (rating);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating, int max) {
        RatingBar view = getView (viewId);
        view.setMax (max);
        view.setRating (rating);
        return this;
    }

    public ViewHolder setTag(int viewId, Object tag) {
        View view = getView (viewId);
        view.setTag (tag);
        return this;
    }

    public ViewHolder setTag(int viewId, int key, Object tag) {
        View view = getView (viewId);
        view.setTag (key, tag);
        return this;
    }

    public ViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable) getView (viewId);
        view.setChecked (checked);
        return this;
    }

    /**
     * 关于单击事件的
     *
     * @param viewId   触发事件的控件id
     * @param listener 单击事件
     */
    public ViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView (viewId);
        view.setOnClickListener (listener);
        return this;
    }

    /**
     * 触摸事件
     *
     * @param viewId   触发事件的控件id
     * @param listener 触摸事件
     * @return
     */
    public ViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = getView (viewId);
        view.setOnTouchListener (listener);
        return this;
    }

    /**
     * 长按事件
     *
     * @param viewId   触发事件的控件id
     * @param listener 长按事件
     * @return
     */
    public ViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
        View view = getView (viewId);
        view.setOnLongClickListener (listener);
        return this;
    }

}
